import router from './router';
import store from './store';
import NProgress from 'nprogress'; // Progress 进度条
import 'nprogress/nprogress.css'; // Progress 进度条样式
import { getToken } from '@/utils/auth'; // 验权

const whiteList = ['/login']; // 不重定向白名单

// permission judge function
function hasPermission(roles, permissionRoles) {
  if (roles.indexOf('admin') >= 0) return true; // admin permission passed directly
  if (!permissionRoles) return true;
  return roles.some((role) => permissionRoles.indexOf(role) >= 0);
}

router.beforeEach(async (to, _from, next) => {
  NProgress.start();
  let hasToken = getToken();

  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' });
      NProgress.done(); // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } else {
      const hasRoles = store.getters.roles && store.getters.roles.length > 0;
      if (hasRoles) {
        // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
        if (hasPermission(store.getters.roles, to.meta.roles)) {
          next();
        } else {
          console.log(401);
          next({ path: '/401', replace: true, query: { noGoBack: true } });
        }
      } else {
        try {
          const {
            //原始的菜单列表数据
            menus,
            //后台构造好的路由
            routers,
            //角色列表
            roles,
          } = await store.dispatch('getInfo');
          //根据后台返回的路由信息调用GenerateRoutes方法生成路由表
          const accessRoutes = await store.dispatch('GenerateRoutes', {
            roles,
            routers,
            menus,
          });
          // 根据roles权限生成可访问的路由表
          // 添加可访问路由表
          router.addRoutes(accessRoutes);
          next({ ...to, replace: true }); // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        } catch (err) {
          console.error(err);
          store.dispatch('FedLogOut').then(() => {
            // Message.error(err || 'Verification failed, please login again')
            next({ path: '/login' });
          });
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next('/login');
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  NProgress.done(); // 结束Progress
});
